#include "keythread.h"
#include "camera_thread.h"
#include "osa.h"

//============================================================================================================
int KEY_A_SHORT;
int KEY_A_LONG;
int KEY_B_SHORT;
int KEY_B_LONG;
unsigned int KEY_VALUE = 0;
unsigned char pressur_flag = 50,pressur_A_real,pressur_B_real;
unsigned char MOTO_A_STATU=0,MOTO_B_STATU =0;
//============================================================================================================
//#define GPIO_HDMI        20       //gpio2_4   HDMI-HPD
//#define GPIO_UP          72       //gpio9_0   OSD上翻
//#define GPIO_DOWN        73       //gpio9_1   OSD下翻
//#define GPIO_LEFT        74       //gpio9_2   OSD向左
//#define GPIO_RIGHT       75       //gpio9_3   OSD向右
//#define GPIO_OK          76       //gpio9_4   OSD确认
//#define GPIO_OSD_CTRL    78       //gpio9_6   OSD开启关闭
//#define GPIO_PHOTO       67       //gpio8_3   ISO-IO1   KEY-A
//#define GPIO_VIDEO       68       //gpio8_4   ISO-IO4   KEY-B
//#define GPIO_RESERV01    67       //gpio8_3   ISO-IO1   IN
//#define GPIO_RESERV02    66       //gpio8_2   ISO-IO2   IN
//#define GPIO_RESERV03    65       //gpio8_1   ISO-IO3   IN
//#define GPIO_RESERV04    68       //gpio8_4   ISO-IO4   IN
//#define GPIO_RESERV05    6        //gpio0_6   ISO-IO5   IN
//#define GPIO_64          64       //gpio8_0   隔离区输出脚,目前没有使用.
//#define GPIO_55          55       //gpio6_7   ISO_IO7   IN
//#define GPIO_88          88       //gpio11_0  ISO_IO8   IN
//#define GPIO_RESERV06    79       //gpio9_7
//#define GPIO_RESERV07    89       //gpio11_1
//#define GPIO_RESERV08    4        //gpio0_4
//#define GPIO_RESERV09    5        //gpio0_5
//#define GPIO_RESERV10    90       //gpio11_2  vi-chan  key[18]
//#define GPIO_RESERV11    80       //gpio10_0  Video    key[19]
//#define GPIO_RESERV12    81       //gpio10_1  MWB      key[20]
//#define GPIO_LED_STROB   87       //gpio10_7
//#define GPIO_MOTO_A      4        //gpio0_4   负压马达开关脚,
//#define GPIO_MOTO_B      5        //gpio0_5   正压马达开关脚,
//#define GPIO_BEEP        1        //gpio0_1   蜂鸣器
//============================================================================================================

#define GPIO_L  0
#define GPIO_H  1
static int key_arr[] =      {GPIO_HDMI,GPIO_PHOTO,GPIO_VIDEO,GPIO_UP,GPIO_DOWN,GPIO_LEFT,GPIO_RIGHT,GPIO_OK,GPIO_OSD_CTRL,
                            GPIO_RESERV01,GPIO_RESERV02,GPIO_RESERV03,GPIO_RESERV04,GPIO_RESERV05,GPIO_RESERV06,GPIO_RESERV07,
                            GPIO_RESERV08,GPIO_RESERV09,GPIO_RESERV10,GPIO_RESERV11,GPIO_RESERV12};
static int gpio_value[] = {GPIO_L ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,
                           GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H};
static struct pollfd fds[32];

//============================================================================================================
//============================================================================================================
// 按键功能定义:0=拍照,1=录像,2=冻结,3=手动白平衡,4=连接摄像头切换;
//============================================================================================================
void function_Key(int key)
{
    static int flag =1;
    ISP_AWB_Calibration_Gain_S stAWBCalibGain;
    switch (key)
    {
        case 0://to take a photo
            OSA_MSG_Put(OSA_PHOTO,0);
            break;
        case 1://to record a video
            OSA_MSG_Put(OSA_VIDEO,0);
            break;
        case 2://to freeze the frame
            flag=!flag;
            camera_video_freezz_frame(flag);
            break;
        case 3://to manual the wb
            printf("start mwb............................................\r\n");
            mppAwbCalibration(0,1,&stAWBCalibGain);
            printf("finish mwb...........................................\r\n");
            //PutIniKeyInt("isp","awb_rgain",stAWBCalibGain.u16AvgRgain-20,PARAM_FILE);
            //PutIniKeyInt("isp","awb_bgain",stAWBCalibGain.u16AvgBgain,PARAM_FILE);

            //2023-05-25 增加两个if判断是什么样的摄像头进行MWB====================================================
            if(global_osd_info.connect_device == 1)
            {
                PutIniKeyInt("isp","sdawb_rgain",stAWBCalibGain.u16AvgRgain-20,PARAM_FILE);
                PutIniKeyInt("isp","sdawb_bgain",stAWBCalibGain.u16AvgBgain,PARAM_FILE);
            }
            if(global_osd_info.connect_device == 2)
            {
                PutIniKeyInt("isp","hdawb_rgain",stAWBCalibGain.u16AvgRgain-20,PARAM_FILE);
                PutIniKeyInt("isp","hdawb_bgain",stAWBCalibGain.u16AvgBgain,PARAM_FILE);
            }
            break;
            
        case 4://连接的摄像头设备  ,sensor_dev:1=ov426,2=OS01A,3=USBcam,4=UHDcam,配合void mppSensorChange(int sensor_dev)函数理解.
            global_osd_info.connect_device = read_osd_parameter_key("Option","connect_device",1);
            switch(global_osd_info.connect_device)
            {
                case 1:
                    global_osd_info.connect_device = 2;
                break;
                case 2:
                    global_osd_info.connect_device = 3;
                break;
                case 3:
                    global_osd_info.connect_device = 4;
                break;
                case 4:
                    global_osd_info.connect_device = 1;
                break;
                default:
                    global_osd_info.connect_device = 1;
                break;
            }
            PutIniKeyInt("Option","connect_device",      global_osd_info.connect_device,PARAM_FILE);
            mppSensorChange(global_osd_info.connect_device);
            //browsform_led_add();
            break;
    }
}

//=======================================================================================================
// 按键初始化
//=======================================================================================================
void keythread_init()
{
    char path[64];
    int gpio_fd[32];
    system("himm 0x112f0064 0x1f1 >/dev/null"); //GPIO9_0 [20P按键扩展口]
    system("himm 0x112f0068 0x1f1 >/dev/null"); //GPIO9_1 [20P按键扩展口]
    system("himm 0x112f006C 0x1f1 >/dev/null"); //GPIO9_2 [20P按键扩展口]
    system("himm 0x112f0070 0x1f1 >/dev/null"); //GPIO9_3 [20P按键扩展口]
    system("himm 0x112f0074 0x1f1 >/dev/null"); //GPIO9_4 [20P按键扩展口]
    system("himm 0x112f007C 0x1f1 >/dev/null"); //GPIO9_6 [20P按键扩展口]
    system("himm 0x112f002C 0x1F4 >/dev/null"); //GPIO8_3 [ISO-IO1,隔离区GPIO]
    system("himm 0x112f0030 0x1F4 >/dev/null"); //GPIO8_4 [ISO-IO4,隔离区GPIO]
    system("himm 0x112f0098 0x1F0 >/dev/null"); //GPIO0-4 [20P按键扩展口]
    system("himm 0x112f009C 0x1F0 >/dev/null"); //GPIO0-5 [20P按键扩展口]
    system("himm 0x112f0034 0x1F0 >/dev/null"); //GPIO0_6 [ISO-IO5,隔离区GPIO,输入]
    system("himm 0x112f0024 0x1F4 >/dev/null"); //GPIO8_1 [ISO-IO3,隔离区GPIO,输入]
    system("himm 0x112f0028 0x1F4 >/dev/null"); //GPIO8_2 [ISO-IO2,隔离区GPIO,输入]
    system("himm 0x112f002C 0x1F4 >/dev/null"); //GPIO8_3 [ISO-IO1,隔离区GPIO,输入]
    system("himm 0x112f0030 0x1F4 >/dev/null"); //GPIO8_4 [ISO-IO4,隔离区GPIO,输入]
    system("himm 0x112f0080 0x1F1 >/dev/null"); //GPIO9_7 [20P按键扩展口]
    system("himm 0x112f0084 0x1F1 >/dev/null"); //GPIO10_0[20P按键扩展口]
    system("himm 0x112f0088 0x1F1 >/dev/null"); //GPIO10_1[20P按键扩展口]
    system("himm 0x112f00AC 0x1F1 >/dev/null"); //GPIO11_1[20P按键扩展口]
    system("himm 0x112f00B0 0x1F1 >/dev/null"); //GPIO11_2[20P按键扩展口]
    system("himm 0x114f0000 0x230 >/dev/null"); //GPIO2_4,HDMI_HOTPLUG[HDMI热插拔检测脚]
    system("himm 0x112f0004 0x200 >/dev/null"); //GPIO10_7[LINGHT_EN,LED横流源,输出]

    gpio_export(GPIO_LED_STROB);
    gpio_direction(GPIO_LED_STROB, 1);
    gpio_write(GPIO_LED_STROB, 0);

    for(int i = 0; i < sizeof(key_arr)/sizeof(int); i++)
    {
        //STEP1导出GPIO::int gpio_export(int pin)   
        gpio_export(key_arr[i]);          

        //STEP2设置输入输出::int gpio_direction(int pin, int dir)       dir: 0-->IN, 1-->OUT
        gpio_direction(key_arr[i], 0);              //

        if(i == 0)
            gpio_edge(key_arr[i], 3);               //key2(HDMI-->HDP)  双边沿触发中断
        else
            gpio_edge(key_arr[i], 2);               //key0 key1  下降沿触发

        memset(path, 0, 64*sizeof(char));
        snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", key_arr[i]);
        gpio_fd[i] = open(path, O_RDONLY);      //open("/sys/class/gpio/gpio192/value",O_RDONLY);
        if(gpio_fd[i] < 0)
        {
            printf("Failed to open of pin=%d to Read value !!\n",i);
            return;
        }
        fds[i].fd = gpio_fd[i];
        fds[i].events  = POLLPRI;
    }

    //MOTOU_PIN_INIT();                               //初始化气泵马达管脚
    //printf("INFO::keythread_init Done !!\n");
}

#if 0      //去掉拍照录像的重复按键
//============================================================================================================
//#define GPIO_HDMI        20       //gpio2_4   HDMI-HPD        IN-----------KEY[00]
//#define GPIO_PHOTO       67       //gpio8_3   ISO-io1         IN-----------KEY[01]    KEY-A   拍照
//#define GPIO_VIDEO       68       //gpio8_4   ISO-io4         IN-----------KEY[02]    KEY-B   录像
//#define GPIO_UP          72       //gpio9_0   OSD_UP          IN-----------KEY[03]      
//#define GPIO_DOWN        73       //gpio9_1   OSD_DOWN        IN-----------KEY[04]
//#define GPIO_LEFT        74       //gpio9_2   OSD_LEFT        IN-----------KEY[05]
//#define GPIO_RIGHT       75       //gpio9_3   OSD_RIGHT       IN-----------KEY[06]
//#define GPIO_OK          76       //gpio9_4   OSD_OK          IN-----------KEY[07]
//#define GPIO_OSD_CTRL    78       //gpio9_6   OSD-ON/OFF      IN-----------KEY[08] 

//#define GPIO_MWB         66       //gpio8_2   ISO-io2         IN-----------KEY[09]    KEY-C   白平衡
//#define GPIO_FREEZZ      65       //gpio8_1   ISO-io3         IN-----------KEY[10]    KEY-D   冻结 
//#define GPIO_ZOOM        6        //gpio0_6   ISO-io5         IN-----------KEY[11]    KEY_E   缩放
//#define GPIO_ISOPIN7     55       //gpio6_7   ISO-io7         IN-----------KEY[12]    Reserv  隔离区预留按键
//#define GPIO_ISOPIN8     88       //gpio11_0  ISO-io8         IN-----------KEY[13]    Reserv  隔离区预留按键1.8V 
//#define GPIO_EXT80       80       //gpio10_0  EXT_20PIN       IN-----------KEY[14]    Reserv  20PIN扩展口   
//#define GPIO_EXT81       81       //gpio10_1  EXT_20PIN       IN-----------KEY[15]    Reserv  20PIN扩展口
//============================================================================================================
//#define GPIO_LED_STROB   87       //gpio10_7                  OUT                     横流源芯片使能脚
//#define GPIO_ISOPIN6     64       //gpio8_0                   OUT                     ISO_IO6   
//#define GPIO_MOTO_A      79       //gpio9_7                   OUT                     负压马达开关脚,
//#define GPIO_MOTO_B      5        //gpio0_5                   OUT                     正压马达开关脚,
//#define GPIO_BEEP        1        //gpio0_1                   OUT                     蜂鸣器
//============================================================================================================

#define GPIO_L  0
#define GPIO_H  1
static int key_arr[] =     {GPIO_HDMI,GPIO_PHOTO,GPIO_VIDEO,GPIO_UP,GPIO_DOWN,GPIO_LEFT,GPIO_RIGHT,GPIO_OK,GPIO_OSD_CTRL,
                            GPIO_MWB,GPIO_FREEZZ,GPIO_ZOOM,GPIO_ISOPIN7,GPIO_ISOPIN8,GPIO_EXT80,GPIO_EXT81};
static int gpio_value[] =  {GPIO_L ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,
                            GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H ,GPIO_H};
static struct pollfd fds[32];

//=======================================================================================================
// 按键初始化
//=======================================================================================================
void keythread_init()
{
    char path[64];
    int gpio_fd[32];

    //输入GPIO
    system("himm 0x114f0000 0x230 >/dev/null"); //GPIO2_4  20                 HDMI_HOTPLUG   key[0]   HDMI热插拔脚
    system("himm 0x112f002C 0x1F4 >/dev/null"); //GPIO8_3  67  [隔离区---IO1]  GPIO_PHOTO     key[1]   KEY-A     
    system("himm 0x112f0030 0x1F4 >/dev/null"); //GPIO8_4  68  [隔离区---IO4]  GPIO_VIDEO     key[2]   KEY-B 
    system("himm 0x112f0064 0x1F1 >/dev/null"); //GPIO9_0  72  [20P按键扩展口]  GPIO_UP        key[3]   OSD上翻 
    system("himm 0x112f0068 0x1F1 >/dev/null"); //GPIO9_1  73  [20P按键扩展口]  GPIO_DOWN      key[4]   OSD下翻         
    system("himm 0x112f006C 0x1F1 >/dev/null"); //GPIO9_2  74  [20P按键扩展口]  GPIO_LEFT      key[5]   OSD向左  
    system("himm 0x112f0070 0x1F1 >/dev/null"); //GPIO9_3  75  [20P按键扩展口]  GPIO_RIGHT     key[6]   OSD向右
    system("himm 0x112f0074 0x1F1 >/dev/null"); //GPIO9_4  76  [20P按键扩展口]  GPIO_OK,       key[7]   OSD确认
    system("himm 0x112f007C 0x1F1 >/dev/null"); //GPIO9_6  78  [20P按键扩展口]  GPIO_OSD_CTRL  key[8]   OSD开启关闭
    system("himm 0x112f0028 0x1F4 >/dev/null"); //GPIO8_2  66  [隔离区---IO2]  GPIO_MWB       key[9]   KEY-C   白平衡
    system("himm 0x112f0024 0x1F4 >/dev/null"); //GPIO8_1  65  [隔离区---IO3]  GPIO_FREEZZ    key[10]  KEY-D   冻结 
    system("himm 0x112f0034 0x1F0 >/dev/null"); //GPIO0_6  6   [隔离区---IO5]  GPIO_ZOOM      key[11]  KEY_E   缩放
    system("himm 0x111F0028 0x1F0 >/dev/null"); //GPIO6_7  55  [隔离区---IO7]  GPIO_ISOPIN7   key[12]  Reserv  隔离区预留按键3.3V    
    system("himm 0x112f0030 0x1F0 >/dev/null"); //GPIO11_0 88  [隔离区---IO8]  GPIO_ISOPIN8   key[13]  Reserv  隔离区预留按键1.8V
    system("himm 0x112f0084 0x1F1 >/dev/null"); //GPIO10_0 80  [20P按键扩展口]  GPIO_EXT80     key[14]  Reserv  20PIN扩展口        
    system("himm 0x112f0088 0x1F1 >/dev/null"); //GPIO10_1 81  [20P按键扩展口]  GPIO_EXT81     key[15]  Reserv  20PIN扩展口  

    //输出GPIO
    system("himm 0x112f0080 0x1F1 >/dev/null"); //GPIO9_7  79  [20P按键扩展口]  GPIO_MOTO_A,   负压马达开关脚,
    system("himm 0x112f009C 0x100 >/dev/null"); //GPIO0-5  5   [20P按键扩展口]  GPIO_MOTO_B,   正压马达开关脚,
    system("himm 0x112f0004 0x100 >/dev/null"); //GPIO10_7 87  [LINGHT_EN  ]  GPIO_LED_STROB,横流源器件使能,
    system("himm 0x112F008C 0x100 >/dev/null"); //GPIO0-1  1                  GPIO_BEEP,     蜂鸣器,
    system("himm 0x112F0020 0x104 >/dev/null"); //GPIO8-0  64  [隔离区---IO6]  GPIO_ISOPIN6,  输出脚预留

    //LED使能脚------高电频------使能.
    gpio_export(GPIO_LED_STROB);
    gpio_direction(GPIO_LED_STROB, 1);
    gpio_write(GPIO_LED_STROB, 1); 

    //GPIO_MOTO_A------低电频.
    gpio_export(GPIO_MOTO_A);
    gpio_direction(GPIO_MOTO_A, 1);
    gpio_write(GPIO_MOTO_A, 0); 

    //GPIO_MOTO_B------低电频.
    gpio_export(GPIO_MOTO_B);
    gpio_direction(GPIO_MOTO_B, 1);
    gpio_write(GPIO_MOTO_B, 0);

    //GPIO_BEEP------低电频.
    gpio_export(GPIO_BEEP);
    gpio_direction(GPIO_BEEP, 1);
    gpio_write(GPIO_BEEP, 0);

    //GPIO_ISOPIN6------低电频.
    gpio_export(GPIO_ISOPIN6);
    gpio_direction(GPIO_ISOPIN6, 1);
    gpio_write(GPIO_ISOPIN6, 0); 

    for(int i = 0; i < sizeof(key_arr)/sizeof(int); i++)
    {
        //STEP1导出GPIO::int gpio_export(int pin)   
        gpio_export(key_arr[i]);          

        //STEP2设置输入输出::int gpio_direction(int pin, int dir)       dir: 0-->IN, 1-->OUT
        gpio_direction(key_arr[i], 0);              //

        if(i == 0)
            gpio_edge(key_arr[i], 3);               //key2(HDMI-->HDP)  双边沿触发中断
        else
            gpio_edge(key_arr[i], 2);               //其他输入IO  下降沿触发

        memset(path, 0, 64*sizeof(char));
        snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", key_arr[i]);
        gpio_fd[i] = open(path, O_RDONLY);          
        if(gpio_fd[i] < 0)
        {
            printf("Failed to open value of pin %d!\n",i);
            return;
        }
        fds[i].fd = gpio_fd[i];
        fds[i].events  = POLLPRI;
    }

    printf("INFO::keythread_init Done !!\n");
}

#endif



//=======================================================================================================
#define KEY_THREAD_DEBUG 0                                             //测试
#if KEY_THREAD_DEBUG
void * keythread_proc(void *pArgs)
{
    int key;
    while(1)
    {
        scanf("%d",&key);
        switch(key)
        {
            case 1: queue_push(&key_queue,1<<0); break;//上
            case 2: queue_push(&key_queue,1<<1); break;//下
            case 3: queue_push(&key_queue,1<<2); break;//左
            case 4: queue_push(&key_queue,1<<3); break;//右
            case 0: queue_push(&key_queue,1<<4); break;//确认
            case 5: queue_push(&key_queue,1<<5); break;//开关
        }
    }
}

#else

//=======================================================================================================
//
//=======================================================================================================
int key_temp[20];
int key_process(int key_indx)
{
    if(key_temp[key_indx] == 0)
    {
        if(gpio_value[key_indx])
        {
            usleep(200);
            if(gpio_value[key_indx]) 
                key_temp[key_indx] = gpio_value[key_indx];
        }
    }
    if(!gpio_value[key_indx] && key_temp[key_indx] )        //判断 KEY B 长按 和 KEY B 短按
    {
        usleep(200);
        if(!gpio_value[key_indx] && key_temp[key_indx])
        {
            int i;
            for(i=0;i<6000;i++)
            {
                usleep(200);
                if(gpio_value[key_indx]) 
                    return 1;
                if(i>100)
                {
                    key_temp[key_indx] = 0;
                    return 2;
                }
            }
        }
    }
    return 0;
}

//=======================================================================================================
//按键及串口功能处理函数
//=======================================================================================================
void * keythread_proc(void *pArgs)
{
    //printf("INFO::(0x%lx): keythread_proc\n",pthread_self());     //打印本线程ID

    unsigned char uart_key_buf[7];
    int hdmivaluetmp;                                               //1= connect,
    int key_tmp[6] = {1,1,1,1,1,1};
    int flag = 0;
    memset(key_temp,1,sizeof(key_temp));
    hdmivaluetmp = gpio_read(key_arr[0]);

    while(1)
    {
        while(queue_size(&key_queue_uart))
        {
            if(queue_from(&key_queue_uart)== 0xfe)                  //读取最前面的一个字节并判断是否为 贞头0XFE 
            {
                if(queue_size(&key_queue_uart)>6)                   //接收到7个数据开始取出并删除数据
                {
                    uart_key_buf[0] = queue_pop(&key_queue_uart);   //取出一个数并删除 第1个字节；
                    uart_key_buf[1] = queue_pop(&key_queue_uart);   //取出一个数并删除 第2个字节；
                    uart_key_buf[2] = queue_pop(&key_queue_uart);   //取出一个数并删除 第3个字节；
                    uart_key_buf[3] = queue_pop(&key_queue_uart);   //取出一个数并删除 第4个字节；
                    uart_key_buf[4] = queue_pop(&key_queue_uart);   //取出一个数并删除 第5个字节；
                    uart_key_buf[5] = queue_pop(&key_queue_uart);   //取出一个数并删除 第6个字节；
                    uart_key_buf[6] = queue_pop(&key_queue_uart);   //取出一个数并删除 第7个字节；

                    //串口协议:包头="0XFE 0XEF 0X00",指令="XX";包尾="0X00 0XED 0XDE"
                    if( (uart_key_buf[0] ==0xfe) &&(uart_key_buf[1] ==0xef) &&(uart_key_buf[2] ==0x00) &&
                        (uart_key_buf[4] ==0x00) &&(uart_key_buf[5] ==0xed) &&(uart_key_buf[6] ==0xde) )
                        {
                            printf("##===============> key:%02X <=============##\r\n",uart_key_buf[3]);
                            switch(uart_key_buf[3])     //判断接收到的指令数据
                            {
                                case 0x01: 
                                        queue_push(&key_queue,1<<0); //读入到 队列buf 缓存区
                                        break;//上
                                case 0x02: 
                                        queue_push(&key_queue,1<<2); //读入到 队列buf 缓存区
                                        break;//左
                                case 0x03: 
                                        queue_push(&key_queue,1<<4); //读入到 队列buf 缓存区
                                        break;//确认
                                case 0x04: 
                                        queue_push(&key_queue,1<<3); //读入到 队列buf 缓存区
                                        break;//右
                                case 0x05: 
                                        queue_push(&key_queue,1<<1); //读入到 队列buf 缓存区
                                        break;//下
                                case 0x06: 
                                        function_Key(0);
                                        break;//拍照
                                case 0x07:
                                        global_osd_info.scale_mode = read_osd_parameter_key("Option", "Size", 3);
                                        // STD模式400-600-800-1000-400…SHD模式800-1000循环
                                        if(global_osd_info.connect_device==1) //ov426
                                        {
                                            global_osd_info.scale_mode =  (global_osd_info.scale_mode%4)+1;
                                        }
                                        else
                                        {
                                            if(global_osd_info.scale_mode==3) global_osd_info.scale_mode = 4;
                                            else global_osd_info.scale_mode = 3;
                                        }
                                        PutIniKeyInt("Option","Size", global_osd_info.scale_mode,PARAM_FILE);
                                        mppFrameScale();
                                        RC_mask(ucguiBuf,global_osd_info.mask_mode -1);
                                        VSB_osd_flush_cache(&osd_mutex);
                                        global_osd_info.scale_mode = read_osd_parameter_key("Option", "Size", 3);
                                        break;//zoom
                                case 0x08: 
                                        global_osd_info.connect_device=1;
                                        PutIniKeyInt("Option","connect_device",global_osd_info.connect_device,PARAM_FILE);
                                        //2023-05-25 同步更新camera参数
                                        PutIniKeyInt("Option","camera",1,PARAM_FILE);   //camera 摄像头类型切换1=OV426  0=OH01A 
                                        /*
                                        //add 2024-04-29------------------------------------------------------------------
                                        //PutIniKeyInt("Option","Size",           2,    PARAM_FILE);     //2 = 600*600.
                                        PutIniKeyInt("Option","Rotate",           0,    PARAM_FILE);     //0 = 旋转0度.
                                        PutIniKeyInt("isp","awb_rgain",           240,  PARAM_FILE);     //R  gain.
                                        PutIniKeyInt("isp","awb_gbgain",          260,  PARAM_FILE);     //Gb gain.
                                        PutIniKeyInt("isp","awb_grgain",          260,  PARAM_FILE);     //Gr gain.
                                        PutIniKeyInt("isp","awb_bgain",           350,  PARAM_FILE);     //B  gain.
                                        PutIniKeyInt("isp","ae_mode",             2,    PARAM_FILE);     //2 =bypass.
                                        PutIniKeyInt("isp","csc_constrast",       45,   PARAM_FILE);     //csc_constrast.
                                        //--------------------------------------------------------------------------------
                                        */
                                        PutIniKeyInt("Option","Rotate",           0,    PARAM_FILE);     //0 = 旋转0度.
                                        PutIniKeyInt("isp","ae_mode",             2,    PARAM_FILE);     //2 =bypass.

                                        mppSensorChange(global_osd_info.connect_device);
                                        PutIniKeyInt("Option","connect_device",global_osd_info.connect_device,PARAM_FILE);
                                        break;//切换到ov426
                                case 0x09: 
                                        global_osd_info.connect_device=2;
                                        PutIniKeyInt("Option","connect_device",global_osd_info.connect_device,PARAM_FILE);
                                        //2023-05-25 同步更新camera参数
                                        PutIniKeyInt("Option","camera",0,PARAM_FILE);   //camera摄像头类型切换1=OV426  0=OH01A 
                                        /*
                                        //add 2024-04-29------------------------------------------------------------------
                                        //PutIniKeyInt("Option","Size",           3,    PARAM_FILE);     //3 = 800*800.
                                        PutIniKeyInt("Option","Rotate",           3,    PARAM_FILE);     //3 = 旋转270度.
                                        PutIniKeyInt("isp","awb_rgain",           245,  PARAM_FILE);     //R  gain.
                                        PutIniKeyInt("isp","awb_gbgain",          260,  PARAM_FILE);     //Gb gain.
                                        PutIniKeyInt("isp","awb_grgain",          260,  PARAM_FILE);     //Gr gain.
                                        PutIniKeyInt("isp","awb_bgain",           243,  PARAM_FILE);     //B  gain.
                                        PutIniKeyInt("isp","ae_mode",             0,    PARAM_FILE);     //0 =自动曝光.
                                        PutIniKeyInt("isp","csc_constrast",       60,   PARAM_FILE);     //csc_constrast.
                                        //--------------------------------------------------------------------------------
                                        */
                                        PutIniKeyInt("Option","Rotate",           3,    PARAM_FILE);     //3 = 旋转270度.
                                        PutIniKeyInt("isp","ae_mode",             0,    PARAM_FILE);     //0 =自动曝光.

                                        mppSensorChange(global_osd_info.connect_device);
                                        PutIniKeyInt("Option","connect_device",global_osd_info.connect_device,PARAM_FILE);
                                        break;//切换到os01a
                                case 0x0a: 
                                        function_Key(1);
                                        break;//录像
                                case 0x0b: 
                                        queue_push(&key_queue,1<<5); 
                                        break;//开关
                                case 0x0c: 
                                        function_Key(3);
                                        break;//白平衡
                                case 0x0f:  
                                        global_osd_info.mask_mode = read_osd_parameter_key("Option", "Mask", 1);
                                        global_osd_info.mask_mode = (global_osd_info.mask_mode % 4) + 1;
                                        RC_mask(ucguiBuf,global_osd_info.mask_mode -1);
                                        PutIniKeyInt("Option","Mask",global_osd_info.mask_mode,PARAM_FILE);
                                        VSB_osd_flush_cache(&osd_mutex);
                                        break;//遮蔽
                                case 0x0e: 
                                        function_Key(0);    //冻结前拍摄一张照片(add by edwin20230207)
                                        video_freeze_state = !video_freeze_state;
                                        break;//冻结

                                case 0x21: //文件拷贝到U盘
                                        printf("SYS-INFO:: Start copy file!!\n");
                                        copyefile = 1;
                                        system("cp -r /picture/* /udisk/picture/");                             //拷贝图片到USB
                                        system("cp -r /video/* /udisk/video");                                  //拷贝视频到USB
                                        copyefile = 0;
                                        printf("SYS-INFO:: Copy file Done!!\n");
                                        system("sync"); 
                                        break;
                                case 0x22: //删除主机内的文件 
                                        printf("SYS-INFO:: Start delete file!!\n");
                                        delete_file = 1;
                                        system("rm -rf /picture/*");                                        //删除EMMC内的图片
                                        system("rm -rf /video/*");                                          //删除EMMC内的视频
                                        delete_file = 0;
                                        printf("SYS-INFO:: Delete file Done!!\n");
                                        system("sync");                                     
                                        break;
                                case 0x23: //系统复位
                                        printf("SYS-INFO:: Start Recovery System!!\n");
                                        system("cp /recovery/capture /usr/qt5.12/demos/");  
                                        system("cp /recovery/multimedia /usr/qt5.12/demos/");  
                                        system("cp /recovery/para.ini /etc/watermask/");  
                                        system("cp /recovery/sensor_rst.sh /usr/qt5.12/demos/");
                                        system("sync"); 
                                        printf("SYS-INFO:: Recovery System Done!!\n");  
                                        system("reboot");
                                        break;                                 
                            }
                        }
                }
            }
            else queue_pop(&key_queue_uart);
        }

        //===========================================================================================================================
        //===========================================================================================================================
        for(int i=0;i<sizeof(key_tmp)/sizeof(int);i++)
        {
            if(key_tmp[i]&&!gpio_value[3+i])
            {
                usleep(200);
                if(key_tmp[i]&&!gpio_value[3+i])
                {
                    int key_count;
                    for(key_count=0;key_count<6000;key_count++)
                    {
                        usleep(200);
                        if(gpio_value[3+i]) 
                            break;
                        if(key_count>100)//长按
                        {
                            if(global_osd_info.mid_select)
                            {
                                while((!gpio_value[3+i]) && queue_empty(&key_queue))
                                {
                                    queue_push(&key_queue,1<<i);
                                    usleep(200);
                                }
                            }
                            break;
                        }
                    }
                    if(key_count<100) queue_push(&key_queue,1<<i);
                }
            }
            key_tmp[i] = gpio_value[3+i];
        }

        //HDMI 热插拔脚状态
        if(hdmivaluetmp!=gpio_value[0])
        {
            usleep(200000);
            if(hdmivaluetmp != gpio_value[0])
            {
                hdmivaluetmp = gpio_value[0];
                camera_displayChange(flag);
            }
        }

        //take picture
        switch(key_process(1))
        {
            case 1:function_Key(KEY_A_SHORT);
                break;
            case 2:function_Key(KEY_A_LONG);
                break;
            default: 
                break;
        }

        //take video
        switch(key_process(2))
        {
            case 1:function_Key(KEY_B_SHORT);
                break;
            case 2:function_Key(KEY_B_LONG);
                break;
            default:
                break;
        }

        //vi_change
        switch(key_process(18))
        {
            case 1: 
            case 2:function_Key(4);
                break;
            default: 
                break;
        }

        //take picture
        switch(key_process(14))
        {
            case 1:  
            case 2: function_Key(0); 
                break;
            default:  
                break;
        }

        //take video
        switch(key_process(19))
        {
            case 1: 
            case 2: function_Key(1); 
                break;
            default: 
                break;
        }

        //mwb
        switch(key_process(20))
        {
            case 1: 
            case 2: function_Key(3); 
                break;
            default: 
                break;
        }
        usleep(200000);
    }
}
#endif

//=======================================================================================================
//按键(uart)线程函数
//=======================================================================================================
void * keythread_run(void *pArgs)
{
    int ret;

    //printf("INFO::(0x%lx): keythread_run \n",pthread_self());     //打印线程
    pthread_t tid;
    keythread_init();
    pthread_create(&tid, 0, keythread_proc, NULL);
    printf("##================================= system init done ==============================##\r\n");
    while(1)
    {
        usleep(10000);
        ret = poll(fds, sizeof(key_arr)/sizeof(int)+1, 400);
        if(ret > 0)
        {
            for(int i=0;i<sizeof(key_arr)/sizeof(int);i++)
            {
                if(fds[i].revents && POLLPRI)
                {
                    lseek(fds[i].fd, 0, SEEK_SET);
                    gpio_value[i] = gpio_read(key_arr[i]);
                }
            }
        }       
    }
}





